﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using SZHL.UI.AppCode;
using FastReflectionLib;
using SZHL.Data;
using Newtonsoft.Json;
using System.Data;

namespace SZHL.UI.API
{
    public class XTDTManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(XTDTManage).GetMethod(msg.Action.ToUpper());
            XTDTManage model = new XTDTManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }
        /// <summary>
        /// 添加系统动态
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDXTDT(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            JH_Auth_XTDT XTDT = JsonConvert.DeserializeObject<JH_Auth_XTDT>(P1);
            if (XTDT.DTLink.Trim() == "")
            {
                msg.ErrorMsg = "系统动态连接不能为空";
                return;
            }
            if (XTDT.ID == 0)
            {
                XTDT.CRDate = DateTime.Now;
                XTDT.CRUser = UserInfo.User.UserName;
                new JH_Auth_XTDTB().Insert(XTDT);
            }
            else
            {
                new JH_Auth_XTDTB().Update(XTDT);
            }
            msg.Result = XTDT;
        }
        /// <summary>
        /// 获取系统动态列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETXTDTLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string UserName = UserInfo.User.UserName;
            if (P1 != "")
            {
                UserName = P1;
            }
            //string strSql = string.Format(" select * from JH_Auth_XTDT where CRUser in ({0})", P1.ToFormatLike());
            //DataTable dt = new JH_Auth_XTDTB().GetDTByCommand(strSql);
            //msg.Result = dt;

            string strWhere = string.Format("  CRUser in ({0})", P1.ToFormatLike());

            int page = 0;
            int.TryParse(context.Request.QueryString["p"] ?? "1", out page);
            page = page == 0 ? 1 : page;
            int total = 0;
            DataTable dtList = new ZH_XTGGB().GetDataPager("JH_Auth_XTDT", "* ", 8, page, "CRDate desc", strWhere, ref total);



            msg.Result = dtList;
            msg.Result1 = total;
        }

        /// <summary>
        /// 删除系统动态
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">ID</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELXTDTBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                if (!new JH_Auth_XTDTB().Delete(d => d.ID.ToString() == P1))
                {

                    msg.ErrorMsg = "删除失败";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        /// <summary>
        /// 获取系统动态信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETXTDTMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            msg.Result = new JH_Auth_XTDTB().GetEntity(d => d.ID.ToString() == P1);
        }

    }
}